public class Leetcode1945 {

    public static int getLucky(String s, int k) {
        char[] chars = s.toCharArray();
        StringBuilder number = new StringBuilder();
        for (char c : chars) {
            number.append(c - 'a' + 1);
        }

        String current = number.toString();

        while (k-- >= 0 && current.length() > 1) {
            int next = 0;
            for (char c : current.toCharArray()) {
                next += c - '0';
            }

            current = String.valueOf(next);
        }

        return Integer.parseInt(current);
    }
}
